/**
 * Source file: DateChecker.ejs
 * Copyright (C) 2013 : Quantum Solutions
 * Id: $Id: Function_DateCheckerNoFuture.ejs 118 2013-04-25 07:14:59Z WillieVisagie55@gmail.com $
 * Date: $Date: 2013-04-25 09:14:59 +0200 (Thu, 25 Apr 2013) $
 */
 
//This Function is the same as Date checker with no future
function CheckDateNoFutureCheck(pCheckDate,pShowErr,pMinYear){

	if (typeof pShowErr == "undefined" || pShowErr == null) { pShowErr = true; }
	if (typeof pMinYear == "undefined" || pMinYear == null) { pMinYear = 1920; }

	//alert ("pCheckDate = "+pCheckDate);
	
	// accept empty dates
	if (pCheckDate.length == 0) {
		return true;
	}
	
	if (pCheckDate.length != 8) { 
	    alert ("Date is wrong length=" + pCheckDate.length + " should be 8");
		return false;
	}
   
	var iDay = 99;
	if (pCheckDate.substring(0, 1) == "0") 
		iDay = parseInt(pCheckDate.substring(1, 2));
	else
		iDay = parseInt(pCheckDate.substring(0, 2));
	
	var iMonth = 99;	
	if (pCheckDate.substring(2, 3) == "0")
		iMonth = parseInt(pCheckDate.substring(3, 4));
	else
		iMonth = parseInt(pCheckDate.substring(2, 4));
	
    var iYear = parseInt(pCheckDate.substring(4, 8));
      
// alert("iYear="+iYear+" iMonth="+iMonth+" iDay="+iDay);

    if((iYear > 2099) || (iYear < pMinYear) || isNaN(iYear)){
		if (pShowErr)
			alert("Year is invalid");
        return false;
    }

    if((iMonth > 12) || (iMonth < 1) || isNaN(iMonth)){
		if (pShowErr)
			alert("Month is invalid :"+iMonth);
        return false;
    }

    if((iDay > 31) || (iDay < 1) || isNaN(iDay)){
		if (pShowErr)
			alert("Day is invalid :"+iDay);
        return false;
    }


    switch(iMonth){
    // months with 31 days;
    case 1:
    case 3:
    case 5: 
    case 7: 
    case 8: 
    case 10: 
    case 12: 
        if((iDay > 31) || (iDay < 1)){
			if (pShowErr)
				alert("Date is invalid - Day value incorrect");
            return false;
            
        }
        break;

    // check month with 30 days;
    case 4:
    case 6:
    case 9: 
    case 11: 
        if((iDay > 30) || (iDay < 1)){
			if (pShowErr)
				alert("Date is invalid - Day value incorrect");
            return false;
        }
        break;

    // check feb;
    case 2:   
        if(((iYear % 4) == 0)){
            if((iDay > 29) || (iDay < 1)){
				if (pShowErr)
					alert("Date is invalid - Day value incorrect");
                return false;
            }
        }else{
            if((iDay > 28) || (iDay < 1)){
				if (pShowErr)
					alert("Date is invalid - Day value incorrect");
				return false;
            }
        }
        break;

    }

/*
function CheckDateNoFutureCheck(pCheckDate){

	//alert ("pCheckDate = "+pCheckDate);
	
	// accept empty dates
	if (pCheckDate.length == 0) {
		return true;
	}
	
	if (pCheckDate.length != 8) { 
	    alert ("Date is wrong length=" + pCheckDate.length + " should be 8");
		return false;
	}
   
	var iDay = 99;
	if (pCheckDate.substring(0, 1) == "0") 
		iDay = parseInt(pCheckDate.substring(1, 2));
	else
		iDay = parseInt(pCheckDate.substring(0, 2));
	
	var iMonth = 99;	
	if (pCheckDate.substring(2, 3) == "0")
		iMonth = parseInt(pCheckDate.substring(3, 4));
	else
		iMonth = parseInt(pCheckDate.substring(2, 4));
	
    var iYear = parseInt(pCheckDate.substring(4, 8));
      
// alert("iYear="+iYear+" iMonth="+iMonth+" iDay="+iDay);

    if((iYear > 2050) || (iYear < 1920) || isNaN(iYear)){
        alert("Year is invalid");
        return false;
    }

    if((iMonth > 12) || (iMonth < 1) || isNaN(iMonth)){
        alert("Month is invalid :"+iMonth);
        return false;
    }

    if((iDay > 31) || (iDay < 1) || isNaN(iDay)){
        alert("Day is invalid :"+iDay);
        return false;
    }


    switch(iMonth){
    // months with 31 days;
    case 1:
    case 3:
    case 5: 
    case 7: 
    case 8: 
    case 10: 
    case 12: 
        if((iDay > 31) || (iDay < 1)){
            alert("Date is invalid - Day value incorrect");
            return false;
            
        }
        break;

    // check month with 30 days;
    case 4:
    case 6:
    case 9: 
    case 11: 
        if((iDay > 30) || (iDay < 1)){
            alert("Date is invalid - Day value incorrect");
            return false;
        }
        break;

    // check feb;
    case 2:   
        if(((iYear % 4) == 0)){
            if((iDay > 29) || (iDay < 1)){
                alert("Date is invalid - Day value incorrect");
                return false;
            }
        }else{
            if((iDay > 28) || (iDay < 1)){
                alert("Date is invalid - Day value incorrect");
		return false;
	    }
	}
        break;
    }
}*/
    return true;
}